package com.sunmouse.toolkit.anim;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/8/11.
 */
public class MenuAnimS implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;
    private static final float SPEED = 0.5f;
    private static final float MAX_TRANS = 1-MIN_SCALE;
    private float scaleXFactor;
    private float scaleYFactor;
    private float translateX;

    /**
     * mediumPage p0=0.0 (to right(0-->1);to left(0-->-1))
     * leftPage p0=-1   (to right (-1-->0))
     * rightPage po=1 (to left(1-->0))
     *
     * @param page
     * @param position
     */
    public void transformPage(View page, float position) {
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();
        if (position > 1 || position < -1) return;
        if (position > 0) {
//            Log.e("TAG", page.hashCode() + " , " + position + "");
//            scaleYFactor = Math.max(MIN_SCALE, 1 - position*SPEED);
//            page.setScaleY(scaleYFactor);
//            page.setScaleX(scaleYFactor);
            translateX = Math.min(MAX_TRANS * 3 / 2, position * 3*SPEED);
            page.setTranslationZ(-translateX * pageWidth);

//            for position!=0
            if (position < 0.002f) {
                page.setScaleX(1.0f);
                page.setScaleY(1.0f);
                page.setTranslationZ(0.0f);
            }
//            scaleXFactor = Math.min(MAX_TRANS * 2,position);
//            page.setScaleX(1+scaleXFactor);
        } else if (position < 0) {
            translateX = Math.min(MAX_TRANS / 2, 1 + position*SPEED);
            page.setTranslationX(-translateX * pageWidth);
            scaleXFactor = Math.max(MIN_SCALE, 1 + position*SPEED);
            page.setScaleX(MIN_SCALE);

//            for position!=0
            if (Math.abs(position) > 0.998f) {
                page.setTranslationZ(0.0f);
                page.setScaleX(1.0f);
            }


            Log.e("TAG", page.hashCode() + " , " + position + "");

        }

    }
}
